BEGIN;
CREATE TABLE "Users" (
    "id" serial NOT NULL PRIMARY KEY,
    "email" varchar(100) NOT NULL,
    "name" varchar(100) NOT NULL,
    "password" varchar(200) NOT NULL
)
;
CREATE TABLE "Categories" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL
)
;
CREATE TABLE "TimelineCategories" (
    "id" serial NOT NULL PRIMARY KEY,
    "timeline_id" integer NOT NULL,
    "timelinecategory_id" integer NOT NULL REFERENCES "Categories" ("id") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("timeline_id", "timelinecategory_id")
)
;
CREATE TABLE "Timelines" (
    "id" serial NOT NULL PRIMARY KEY,
    "title" varchar(100) NOT NULL,
    "creator_id" integer NOT NULL UNIQUE REFERENCES "Users" ("id") DEFERRABLE INITIALLY DEFERRED,
    "initial_date" timestamp with time zone NOT NULL,
    "final_date" timestamp with time zone NOT NULL,
    "time_unity" integer NOT NULL,
    "privacy" integer NOT NULL
)
;
ALTER TABLE "TimelineCategories" ADD CONSTRAINT "timeline_id_refs_id_bd144e9f" FOREIGN KEY ("timeline_id") REFERENCES "Timelines" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "ResourceTypes" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL
)
;
CREATE TABLE "Resources" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(100) NOT NULL,
    "url" varchar(200) NOT NULL,
    "resource_type_id" integer NOT NULL UNIQUE REFERENCES "ResourceTypes" ("id") DEFERRABLE INITIALLY DEFERRED
)
;
CREATE TABLE "EventAuthor" (
    "id" serial NOT NULL PRIMARY KEY,
    "event_id" integer NOT NULL,
    "user_id" integer NOT NULL REFERENCES "Users" ("id") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("event_id", "user_id")
)
;
CREATE TABLE "EventResources" (
    "id" serial NOT NULL PRIMARY KEY,
    "event_id" integer NOT NULL,
    "resource_id" integer NOT NULL REFERENCES "Resources" ("id") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("event_id", "resource_id")
)
;
CREATE TABLE "Events" (
    "id" serial NOT NULL PRIMARY KEY,
    "title" varchar(200) NOT NULL,
    "status" boolean NOT NULL,
    "description" varchar(1000) NOT NULL,
    "date" timestamp with time zone NOT NULL
)
;
ALTER TABLE "EventAuthor" ADD CONSTRAINT "event_id_refs_id_1793d3b2" FOREIGN KEY ("event_id") REFERENCES "Events" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "EventResources" ADD CONSTRAINT "event_id_refs_id_ea6b0367" FOREIGN KEY ("event_id") REFERENCES "Events" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;